/**
 * 
 */
package com.wistech.search.solr.bench;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.junit.Test;

/**
 * 
 * 测试HashMap和ConcurrentHashMap的并发性能区别 。
 * 
 * 仿照100个并发，Map里10w数据，每个测试10w次get操纵 ，循环10轮。
 * 
 * @author Liuye
 * 
 */
public class MapTest2 {
	static final int threads = 100;
	static final int NUMBER = 100000;
	static final int REPEAT = 10;

	@Test
	public void testClone() throws Exception {
		Map<String, Object> defaultValue = new HashMap<String, Object>(threads);
		for (int e = 0; e < threads; e++) {
			defaultValue.put(String.valueOf(e), e);
		}
		long startTime = System.nanoTime();
		for (int i = 0; i <= REPEAT; i++) {
			for (int j = 0; j < NUMBER; j++) {
				Map<String, Object> defaultValue2 = new HashMap<String, Object>(
						threads);
				defaultValue2.putAll(defaultValue);
			}
		}
		long endTime = System.nanoTime();
		System.out.println("使用putall创建耗费" + (endTime - startTime) + "纳秒.");
	}

	@Test
	public void testNew() throws Exception {
		long startTime = System.nanoTime();
		for (int i = 0; i <= REPEAT; i++) {
			for (int j = 0; j < NUMBER; j++) {
				Map<String, Object> defaultValue = new HashMap<String, Object>(
						threads);
				for (int e = 0; e < threads; e++) {
					defaultValue.put(String.valueOf(e), e);
				}
			}
		}
		long endTime = System.nanoTime();
		System.out.println("使用put创建耗费" + (endTime - startTime) + "纳秒.");
	}

}